package org.openzip.openzip.unpacking.view.swing;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.openzip.openzip.unpacking.model.IOpenZipUnpacker;
import org.openzip.openzip.unpacking.view.IUnpackerUI;

public class UnpackerGUI implements IUnpackerUI
{
	private IOpenZipUnpacker logic;
	private JFrame mainWindow;
	private JTextField destinationField;
	private JButton browseButton;
	/*private CheckboxedTree fileTree;
	private JButton selectAllButton;
	private JButton deselectAllButton;*/
	private JButton closeButton;
	private JButton unpackButton;
	
	//private SelectedFilesModel selectionModel;
	
	private Dimension preferredButtonSize = new Dimension(100, 25);
	
	public UnpackerGUI(){
		prepareMainWindow();
		Box b = new Box(BoxLayout.Y_AXIS);
		b.add(prepareTopPanel());
		b.add(new JSeparator());
		b.add(prepareBottomPanel());
		/*StandardForm form = new StandardForm(prepareTopPanel(), 
					//prepareMiddlePanel(), prepareBottomPanel());
					null, prepareBottomPanel());*/
		b.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		mainWindow.setContentPane(b);
		mainWindow.pack();
		mainWindow.setLocationRelativeTo(null);
	}

	private Component prepareTopPanel(){
		destinationField = new JTextField();
		destinationField.setPreferredSize(new Dimension(350, 25));
		destinationField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
		browseButton = new JButton("Browse...");
		browseButton.setPreferredSize(preferredButtonSize);
		browseButton.addActionListener(new FileActionListener());
		
		JPanel panel = new JPanel(new BorderLayout(5, 0));
		panel.add(preparePanel("Destination", destinationField));
		panel.add(preparePanel(null, browseButton), BorderLayout.EAST);
		
		panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
		
		return panel;
	}

	/*private Component prepareMiddlePanel(){
		fileTree = new CheckboxedTree(new File("d:\\Sdílené dokumenty\\D&D\\"), false);
		selectionModel = new SelectedFilesModel();
		fileTree.setGlobalSelectionModel(selectionModel);
		
		selectAllButton = new JButton("Select all");
		selectAllButton.setPreferredSize(preferredButtonSize);
		deselectAllButton = new JButton("Deselect all");
		deselectAllButton.setPreferredSize(preferredButtonSize);
		
		JPanel panel = new JPanel(new BorderLayout(5, 0));
			JPanel treePanel = new JPanel(new BorderLayout());
			treePanel.add(new JLabel("Files to unpack:"), BorderLayout.NORTH);
			treePanel.add(new JScrollPane(fileTree));
		panel.add(treePanel);
			Box buttonPanel = new Box(BoxLayout.Y_AXIS);
			buttonPanel.add(Box.createRigidArea(new Dimension(0, 20)));
			buttonPanel.add(selectAllButton);
			buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));
			buttonPanel.add(deselectAllButton);
		panel.add(buttonPanel, BorderLayout.EAST);

		panel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
		
		return panel;			
	}*/

	private Component prepareBottomPanel(){
		closeButton = new JButton("Close");
		
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String message = "Are you sure you want to quit and forget"
					+ " all your selected files?";
				int userChoice = JOptionPane.showOptionDialog(null, message, 
						"Confimation", JOptionPane.YES_NO_OPTION, 
						JOptionPane.QUESTION_MESSAGE, null, null, 
						JOptionPane.INITIAL_VALUE_PROPERTY);
				if (userChoice == JOptionPane.YES_OPTION) {System.exit(0);}
			}
		});
		
		unpackButton = new JButton("Unpack all");
		unpackButton.addActionListener(new UnpackListener());
		
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		panel.add(closeButton);
		panel.add(Box.createRigidArea(new Dimension(5, 0)));
		panel.add(unpackButton);

		panel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
		
		return panel;
	}

	private Container preparePanel(String label, JComponent comp){
		Box panel = new Box(BoxLayout.Y_AXIS);
		panel.add(Box.createVerticalGlue());

		if(label != null){
			JLabel labelText = new JLabel(label);
			labelText.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			panel.add(labelText);
			// panel.add(Box.createRigidArea(gaps));
		}

		comp.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		panel.add(comp);

		return panel;
	}
	
	private void prepareMainWindow(){
		mainWindow = new JFrame("OPEN Zip - Unpack");
		mainWindow.setResizable(false);
		mainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	@Override public void showGui(IOpenZipUnpacker logic){
		this.logic = logic;
		mainWindow.setVisible(true);
	}

	@Override public void showGui(IOpenZipUnpacker logic, 
				Collection<ZipEntry> files){
		throw new UnsupportedOperationException(
					"This functionality is not implemented");
		/*List<ZipEntry> list = new ArrayList<ZipEntry>();
		for(ZipEntry e : files){
			list.add(e);
		}
		Collections.sort(list, new Comparator<ZipEntry>(){
			@Override public int compare(ZipEntry o1, ZipEntry o2){
				return o1.getName().compareTo(o2.getName());
			}
		});
		for(ZipEntry e : list){
			System.out.println(e.getName());
		}*/
	}
	
	private class UnpackListener implements ActionListener
	{

		@Override public void actionPerformed(ActionEvent e){
			logic.unpack(new File(destinationField.getText()), null);
		}
		
	}
	
	
	/**
	 * Listener for the Browse... button.
	 */
	private class FileActionListener implements ActionListener
	{
		/**
		 * When clicked the Browse... button, a default <code>JFileChooser</code>
		 * will be displayed. After the user picks a file, the filename and file
		 * path will be displayed in the fields.
		 */
		@Override public void actionPerformed(ActionEvent e){
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			if(fileChooser.showOpenDialog(mainWindow) == 
				JFileChooser.APPROVE_OPTION){
				File f = fileChooser.getSelectedFile();
				destinationField.setText(f.getPath());
			}
		}
	}
	
	public static void main(String[] args) throws ZipException, IOException{
		UnpackerGUI gui = new UnpackerGUI();
		/*File zip = new File("D:\\Sdílené dokumenty\\Visual Studio 2008\\" + 
		  "Projects\\VŠE_xzavj15_RSS.zip");
		ZipFile file = new ZipFile(zip);
		Enumeration e = file.entries();
		Collection<ZipEntry> col = new ArrayList<ZipEntry>();
		while(e.hasMoreElements()){
			col.add((ZipEntry)e.nextElement());
		}*/
		gui.showGui(null);
	}
}
